<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >
        {{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="submit">确定</el-button>
      <el-button>取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRole } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadrole()
  },
  methods: {
    async submit() {
      try {
        await assignRoles({ id: this.curId, roleIds: this.roleIds })
        // 关闭弹层
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    async loadrole() {
      try {
        const res = await getRole({ page: 1, pagesize: 100 })
        console.log('res', res)
        const detail = await getUserDetailById(this.curId)
        // console.log(this.curId)
        console.log(detail)
        this.roleIds = detail.data.roleIds
        this.list = res.data.rows
        console.log(this.list)
        // 保存数据
      } catch (err) {
        console.log('err', err)
      }
    }
  }
}
</script>
